package de.sp.games.snake12k;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class GameplayState extends BasicGameState
{

	private int stateID = 1;
	private float screenWidth;
	private float screenHeight;

	public static float fieldX = 16;
	public static float fieldY = 16;

	private boolean pause = false;
	private Image pauseImage;

	private UnicodeFont uFSmaller;

	public GameplayState(int stateID)
	{
		this.stateID = stateID;
	}

	@Override
	public int getID()
	{
		return stateID;
	}

	Field field;

	@Override
	public void init(GameContainer gc, StateBasedGame arg1) throws SlickException
	{
		screenWidth = gc.getWidth();
		screenHeight = gc.getHeight() - 30;
		pauseImage = new Image("ressource/pause.png");

		field = new Field();

		uFSmaller = new UnicodeFont(new java.awt.Font("Verdana", java.awt.Font.PLAIN, 18));
		uFSmaller.getEffects().add(new ColorEffect(java.awt.Color.ORANGE));
		uFSmaller.addAsciiGlyphs();
		uFSmaller.loadGlyphs();
	}

	@Override
	public void render(GameContainer gc, StateBasedGame arg1, Graphics g) throws SlickException
	{
		/* Field */
		g.scale((1.0f / (fieldX * 16.0f)) * screenWidth, (1.0f / (fieldY * 16.0f)) * screenHeight);
		field.draw(g);

		g.resetTransform();
		if (pause)
		{
			pauseImage.draw((gc.getWidth() / 2) - (pauseImage.getWidth() / 2), (gc.getHeight() / 2) - (pauseImage.getHeight() / 2) - 20);

			String optionsString = "Press: 'R' for Restart - 'ESC' for Menu";
			uFSmaller.drawString((gc.getWidth() / 2) - (uFSmaller.getWidth(optionsString) / 2), gc.getHeight() - 27, optionsString);
		}

		if (!pause)
		{
			String gameString = "SCORE: " + SUtil.actualScore() + "     Time: " + SUtil.getPlayedTime() + "     Snake Length: " + (SUtil.snakeLength + 1) + "    'P' - Pause";
			uFSmaller.drawString((gc.getWidth() / 2) - (uFSmaller.getWidth(gameString) / 2), gc.getHeight() - 27, gameString);
		}
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int arg2) throws SlickException
	{
		Input input = gc.getInput();

		if (input.isKeyPressed(Input.KEY_F12))
		{
			if (gc.isFullscreen())
			{
				gc.setFullscreen(false);
			}
			else
			{
				gc.setFullscreen(true);
			}
		}

		if (input.isKeyPressed(Input.KEY_R))
		{
			pause = false;
			SUtil.snakeLength = SUtil.startSnakeLength;
			SUtil.resetPlayedTime();
			SUtil.stopPlayedTime();
			SUtil.resetTimeMoveCounter();
			SUtil.gameOver = false;
			SUtil.fieldResetWithNewField();
			input.clearKeyPressedRecord();
		}
		if (input.isKeyPressed(Input.KEY_ESCAPE))
		{
			if (!pause)
			{
				sbg.enterState(Snake12k.TITLEMENUSTATE);
				SUtil.snakeLength = SUtil.startSnakeLength;
				SUtil.resetPlayedTime();
				SUtil.resetTimeMoveCounter();
				SUtil.gameOver = false;
				SUtil.fieldResetWithNewField();
				input.clearKeyPressedRecord();
			}
			else
			{
				sbg.enterState(Snake12k.TITLEMENUSTATE);
				input.clearKeyPressedRecord();
			}
		}

		if (input.isKeyPressed(Input.KEY_P))
		{
			if (pause)
			{
				pause = false;
			}
			else
			{
				pause = true;
			}
		}

		if (SUtil.gameOver)
		{
			sbg.enterState(2);
			SUtil.stopPlayedTime();
			input.clearKeyPressedRecord();
		}
		if (!pause)
		{
			SUtil.incrementTimeMoveCounter();
			field.update(gc);
		}

		if (SUtil.isTimeMove())
		{
			SUtil.resetTimeMoveCounter();
			SUtil.incrementPlayedTime();
		}
	}

}
